package com.sky.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author
 * @Date 2024/5/17 19:39
 * @Description:
 */
@Component
@Slf4j
public class OrderTask {
    @Autowired
    private OrderMapper orderMapper;
    @Scheduled(cron = "0 * * * * *")
    public void afterTask(){
        LocalDateTime after=LocalDateTime.now().minusMinutes(15);
        log.info("定时查询订单是否过期:{}",LocalDateTime.now());
        Orders order=Orders.builder()
                .status(Orders.CANCELLED)
                .cancelTime(LocalDateTime.now())
                .cancelReason("订单超时")
                .build();
        LambdaUpdateWrapper <Orders> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(Orders::getStatus,Orders.PENDING_PAYMENT)
                     .ge(Orders::getOrderTime,after);
        orderMapper.update(order,updateWrapper);
    }
    @Scheduled(cron = "0 0 0 * * *")
    public void everydayUpdateTask(){
    log.info("定时查询订单是否未确定已完成:{}",LocalDateTime.now());
    LocalDateTime daytime=LocalDateTime.now().minusDays(1);
    Orders order=Orders.builder()
                       .status(5)
                       .build();
    LambdaUpdateWrapper <Orders> updateWrapper=new LambdaUpdateWrapper<>();
    updateWrapper.eq(Orders::getStatus,Orders.DELIVERY_IN_PROGRESS)
                 .le(Orders::getOrderTime,daytime);
    orderMapper.update(order,updateWrapper);
    }
}
